#include "Vertex3D.h"
#include "Util.h"

Vertex3D::Vertex3D() {}

Vertex3D::Vertex3D
(const Vertex3D& v)
{
  GLdouble pos[3] = {v.x(), v.y(), v.z()};
  copy(pos, pos + 3, _pos);
  nFaces = 0;
}

Vertex3D::Vertex3D
(const GLdouble pos[3])
{
  nFaces = 0;
  copy(pos, pos + 3, _pos);
}

Vertex3D::~Vertex3D () {}

bool
Vertex3D::operator==
(const Vertex3D& v)
{
  return (v.x() == _pos[0] and v.y() == _pos[2] and v.z() == _pos[3]);
}

void
Vertex3D::push(GLdouble *normalface)
{
  normalfaces.push_back(normalface);
  nFaces++;
}

void
Vertex3D::computeNormal ()
{
  _normal[0] = 0;
  _normal[1] = 0;
  _normal[2] = 0;
  for(int i=0; i<nFaces; i++){
      _normal[0] += normalfaces[i][0];
      _normal[1] += normalfaces[i][1];
      _normal[2] += normalfaces[i][2];
  }
  _normal[0] /= nFaces;
  _normal[1] /= nFaces;
  _normal[2] /= nFaces;

  normalize(_normal);
}

GLdouble Vertex3D::x() const {return _pos[0];}
GLdouble Vertex3D::y() const {return _pos[1];}
GLdouble Vertex3D::z() const {return _pos[2];}

GLdouble* Vertex3D::pos() {return _pos;}
GLdouble* Vertex3D::normal() {return _normal;}
